package com.bungaurs.sdk;

import com.bungarus.codec.protobuf.MessageProtobuf;
import com.bungarus.sdk.SmarterClient;
import com.bungarus.sdk.listener.ClientMessageListener;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by tang on 2019/1/31.
 */
public class SmartClientTests2 extends SmarterClient {

    public static void main(String[] args) {
        SmartClientTests2 client = new SmartClientTests2();
        client.addListener(new BusiProcessingListener());
        client.smartConnect(SmarterClient.DEFAULT_CALLBACK);
        MessageProtobuf.Message message;

        while (true) {
            String line = getInputString();
            if(line.equalsIgnoreCase("login")) { //login
                message = TestClientMessageBuilder.buildLoginMessage(
                        "1006",
                        10000,
                        "Apo",
                        "Device",
                        "username6",
                        "password6"
                );
            }
            else if(line.equalsIgnoreCase("logout")) { // logout
                message = TestClientMessageBuilder.buildLogoutMessage(
                        client.getLoggedIn(),
                        "Apo",
                        "Device"
                );
            }
            else { // IM message
                message = TestClientMessageBuilder.buildIMMessage(
                        client.getLoggedIn(),
                        "1001",
                        "Apo",
                        "Device",
                        line
                );
            }
            // sending
            client.send(message, value -> {
                System.err.println("Message sent: " + value);
                return null;
            });
        }
    }

    public static String getInputString(){
        String line = "";
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            line = reader.readLine();
        }catch(IOException e){
            e.printStackTrace();
        }
        return line;
    }

    @Override
    public MessageProtobuf.Message makeloginMessage() {
        return TestClientMessageBuilder.buildLoginMessage(
                "1006",
                10000,
                "Apo",
                "Device",
                "username6",
                "password6"
        );
    }

    private static class BusiProcessingListener implements ClientMessageListener {
        Logger logger = Logger.getLogger(BusiProcessingListener.class);
        @Override
        public void onStatus(MessageProtobuf.Message status) {
            logger.debug("BusiProcessingListener onStatus \n" + status.toString());
        }

        @Override
        public void onIM(MessageProtobuf.Message chatMessage) {
            logger.debug("BusiProcessingListener onIM + \n" + chatMessage.toString());
        }

        @Override
        public void onGroupIM(MessageProtobuf.Message groupMessage) {
            logger.debug("BusiProcessingListener onGroupIM \n" + groupMessage.toString());
        }

        @Override
        public void onOthers(MessageProtobuf.Message otherMessage) {
            logger.debug("BusiProcessingListener onOthers \n" + otherMessage.toString());
        }

        @Override
        public void onAuthExpire() {
            logger.debug("BusiProcessingListener onAuthExpire \n");
        }

        @Override
        public void onUserNotExist() {
            logger.debug("BusiProcessingListener onUserNotExist \n");
        }
    }
}
